package com.tarena.lbs.user.controller;


import com.tarena.lbs.user.pojo.param.RoleAssignmentParam;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处理和后台用户相关的业务接口
 */

@Slf4j
@RestController
@Tag(name = "后台用户接口", description = "处理和后台用户相关的业务接口")
public class AdminController {

    @Autowired
    private LoginService loginService;


    @Autowired
    private AdminService adminService;


    @Operation(summary = "管理员登录")
    @PostMapping("/passport/admin/login")
    public Result<LoginVO> adminLogin(AdminLoginParam param) throws BusinessException {
        log.debug("管理员登录业务处理,入参:{}",param);
        LoginVO vo = loginService.doLogin(param);
        log.debug("管理员登录成功，入参:{},出参:{}",param,vo);
        return new Result<>(vo);
    }


    @Operation(summary = "用户详情")
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> detail() throws BusinessException {
        log.debug("查询登录用户详情");
        AdminVO vo = adminService.detail();
        log.debug("查询登录用户详情成功，出参:{}",vo);
        return new Result<>(vo);
    }

    /**
     * 查询后台管理的 账号列表
     */
    @Operation(summary = "账号列表 description = 支持按用户名、手机号和商家ID进行查询的分页接口")
    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> pageList(@RequestBody AdminQuery query)
        throws BusinessException{
        log.info("接收账号列表查询请求，参数：{}", query);
        PageResult<AdminVO> voPage = adminService.pageList(query);
        log.info("账号列表查询成功，返回条数：{}", voPage.getObjects() != null ? voPage.getObjects().size() : 0);
        return new Result<>(voPage);
    }


    @Operation(summary = "账号保存", description = "支持创建平台账号和商家账号")
    @PostMapping("/admin/basic/role/add")
    public Result<Void> save(@RequestBody AdminParam param) throws BusinessException{
        log.debug("保存管理员账号，入参:{}", param);
        adminService.save(param);
        log.debug("保存管理员账号成功");
        return Result.success();
    }

    @Operation(summary = "账号更新")
    @PostMapping("/admin/basic/role/update")
    public Result<Void> update(
            @RequestParam("id") Integer id,
            @RequestParam("accountType") Integer accountType,
            @RequestParam(value = "businessId", required = false) Integer businessId,
            @RequestParam("accountPhone") String accountPhone,
            @RequestParam("accountPassword") String accountPassword,
            @RequestParam("nickname") String nickname,
            @RequestParam("email") String email,
            @RequestParam("accountStatus") Integer accountStatus
    ) throws BusinessException {
        // 创建AdminParam对象并设置参数
        AdminParam param = new AdminParam();
        param.setId(id);
        param.setAccountType(accountType);
        param.setBusinessId(businessId);
        param.setAccountPhone(accountPhone);
        param.setAccountPassword(accountPassword);
        param.setNickname(nickname);
        param.setEmail(email);
        param.setAccountStatus(accountStatus);

        log.debug("更新管理员账号，入参:{}", param);
        adminService.update(param);
        log.debug("更新管理员账号成功");
        return Result.success();
    }

    @Operation(summary = "账号删除")
    @PostMapping("/admin/basic/role/delete")
    public Result<Void> delete(@RequestParam("id") Integer id) throws BusinessException {
        log.info("删除管理员账号，ID:{}", id);
         //参数校验
        if (id == null || id <=0) {
            return Result.fail("-1","ID不能为空");
        }
        //调用服务层执行删除
       return adminService.deleteAdmin(id);

    }

    @Operation(summary = "修改账号状态")
    @PostMapping("/admin/basic/role/status/{id}/{status}")
    public Result<Void> updateStatus(@PathVariable Integer id, @PathVariable Integer status)
            throws BusinessException{
        log.debug("修改管理员账号状态，ID:{}, 状态:{}", id, status);
        adminService.updateStatus(id, status);
        log.debug("修改管理员账号状态成功");
        return Result.success();
    }

    @Operation(summary = "分配角色")
    @PostMapping("/admin/basic/role/assignRole")
    public Result<Void> assignRole(
            @RequestParam("userId") Integer userId, 
            @RequestParam("accountType") Integer accountType, 
            @RequestParam(value = "businessId", required = false) Integer businessId) throws BusinessException {
        log.debug("为管理员分配角色，入参: userId={}, accountType={}, businessId={}", userId, accountType, businessId);
        
        // 参数校验
        if (userId == null || userId <= 0) {
            throw new BusinessException("-1", "ID不能为空");
        }
        if (accountType == null || (accountType != 0 && accountType != 1)) {
            throw new BusinessException("-1", "角色类型无效");
        }
        // 如果是商家管理员，businessId不能为空
        if (accountType == 1 && businessId == null) {
            throw new BusinessException("-1", "商家管理员必须关联商家ID");
        }
        
        adminService.assignRole(userId, accountType, businessId);
        log.debug("为管理员分配角色成功");
        return Result.success();
    }

    @Operation(summary = "重置密码")
    @PostMapping("/admin/basic/role/resetPassword")
    public Result<Void> resetPassword(@RequestParam("id") Integer id)
            throws BusinessException{
        log.debug("重置管理员密码，ID:{}", id);
        // 参数校验
        if (id == null || id <= 0) {
            throw new BusinessException("-1", "ID不能为空");
        }
        adminService.resetPassword(id);
        log.debug("重置管理员密码成功");
        return Result.success();
    }












































}